//@ts-ignore
import * as lib from "libwebdav.so";
import { FileInfo } from "./FileInfo";
import { AuthType } from "./AuthType";

export class Client {
  private client: lib.Client | undefined;

  constructor(
    webdavUrl: string,
    userName?: string,
    password?: string,
    authType?: AuthType
  ) {
    this.client = new lib.Client(webdavUrl, userName, password, authType);
  }

  connect(
    debugMode: boolean,
    ignoreCert: boolean,
    serverCert?: string
  ): Promise<void> {
    return this.client?.connect(debugMode, ignoreCert, serverCert);
  }

  list(remotePath: string): Promise<Array<FileInfo>> {
    return this.client?.list(remotePath);
  }

  get(remotePath: string): Promise<ArrayBuffer | null> {
    return this.client?.get(remotePath);
  }

  writeTo(remotePath: string, filePath: string): Promise<boolean> {
    return this.client?.writeTo(remotePath, filePath);
  }

  put(remotePath: string, filePath: string): Promise<boolean> {
    return this.client?.put(remotePath, filePath);
  }

  delete(remotePath: string): Promise<boolean> {
    return this.client?.delete(remotePath);
  }

  mv(sourcePath: string, targetPath: string): Promise<boolean> {
    return this.client?.mv(sourcePath, targetPath);
  }

  cp(sourcePath: string, targetPath: string): Promise<boolean> {
    return this.client?.cp(sourcePath, targetPath);
  }

  mkdir(remotePath: string): Promise<boolean> {
    return this.client?.mkdir(remotePath);
  }

  close() {
    this.client.close();
    this.client = undefined;
  }
}
